<?php
//Memcach缓存类
class MemcacheAction
{
    private $host = "127.0.0.1";
    private $port = 11211;
    private $timeout = 60;
    private $lifetime = 3600;
    private static $link = null;
    var $memcache;

    public function __construct()
    {
        $this->_init();
        $this->memcache = new Memcache();
        $this->memcache->connect($this->host, $this->port, $this->timeout) or die('can not connect Memcache => '. $this->host." port => ".$this->port);
    }
    
    private function _init(){
        global $CONF;
        if($CONF['memcache']['host']){
            $this->host = $CONF['memcache']['host'];
        }
        if($CONF['memcache']['port']){
            $this->port = $CONF['memcache']['port'];
        }
        if($CONF['memcache']['timeout']){
            $this->timeout = $CONF['memcache']['timeout'];
        }
        if($CONF['memcache']['lifetime']){
            $this->lifetime = $CONF['memcache']['lifetime'];
        }
    }
    private function __clone(){}

    public static function getCache(){
        if(self::$link==null){
            self::$link = new MemcacheAction();
        }
        return self::$link;
    }

    function get($name)
    {
        return $this->memcache->get($name);
    }

    function set($name, $value, $ttl = 0)
    {
        if(!$ttl) $ttl = $this->lifetime;
        return ($this->memcache->set($name, $value, 0, $ttl) or die('fail save to server'));
    }

    function replace($name,$value,$ttl){
        if(!$ttl) $ttl = $this->lifetime;
        return $this->memcache->replace($name, $value, 0, $ttl);
    }

    function rm($name)
    {
        return $this->memcache->delete($name);
    }

    function clear()
    {
        return $this->memcache->flush();
    }
}
?>